<template>
  <el-form class="add-event-vote" v-if="wbData">
    <el-divider content-position="center">全局票数设置</el-divider>
    <input-normal label="每个用户每天分配票数" v-model="wbData.day_limit" @change="onUpdateWbComponent"></input-normal>
    <switch-normal v-model="wbData.isUid" label="个人行为" @change="onUpdateWbComponent"></switch-normal>
    <input-normal label="系数" v-model="wbData.vote_coefficient"  @change="onUpdateWbComponent"></input-normal>
    <input-normal label="触发投票关联id" v-model="wbData.syn_com_id" @change="onUpdateWbComponent"></input-normal>
    <textarea-normal v-if="wbData.syn_com_id && wbData.syn_com_id.length > 0" label="关联投票ids" placeholder="请在此处输入关联的ids" v-model="wbData.syn_vote_ids" @change="onUpdateWbComponent"></textarea-normal>
    <el-collapse v-model="activeNames">
      <el-collapse-item v-for="(item, key, index) in eventWbLists" :name="index+1" :key="index">
          <template #title>
          投票列表项{{index+1}}
        </template>
        <input-normal class="margin-top-10" label="调整投票基数" placeholder="请在此处输入调整投票基数" v-model="item.base" @change="onUpdateWbItem(key)"></input-normal>
        <input-normal label="限制投票数" placeholder="请在此处输入限制投票数" v-model="item.total" @change="onUpdateWbItem(key)"></input-normal>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>

<script setup lang="ts">
import { useWebControlsStore } from '@/store/web/controls'
import { useWebComponentStore } from '@/store/web/component'
import { getWbListByItem } from '@/components/utils'

const props = defineProps<{ 
  event: IEvent,
  index: number,
}>()

const event = props.event
const useWebControls = useWebControlsStore()
const useWebComponent = useWebComponentStore()

const  activeNames = [1]
let wbData_: any
const wbData = computed(() => {
  try {
    wbData_ = useWebControls.currentControls![event.controlId] && useWebControls.currentControls![event.controlId].data
  } catch (error) {
    console.log("controlData.data null")
  }
  return wbData_
})
let wbLists_: any[]
const eventWbLists = computed(() => {
  if(wbData_.elements) {
    wbLists_ = wbData_.elements
  }
  return wbLists_
})
const onUpdateWbComponent = () => {
  useWebControls.updateWbComponent({ controlId: event.controlId, data: wbData_ })
}
const onUpdateWbItem = (index: number) => {
  const controlId = event.controlId
  const wbList = getWbListByItem(useWebComponent.currentComponentData!)
  const params = {item: wbList[index], index, controlId}
  useWebControls.updateWbComponentItem(params)
}
</script>
